﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Xml;

namespace NullSuicide
{
    public static class DataConnector
    {

        internal static Dictionary<int, Zone> LoadItems()
        {
            string xml_adress = "http://junemann.nl/null/getitems.php";
            XmlDocument doc = new XmlDocument();
            doc.Load(xml_adress);

            Dictionary<int, Zone> zones = new Dictionary<int, Zone>();

            foreach (XmlNode zoneNode in doc.ChildNodes[1].ChildNodes)
            {
                // Zones
                int zoneID = int.Parse(zoneNode.Attributes["id"].Value);
                string zoneName = zoneNode.Attributes["name"].Value;

                Zone zone = new Zone(zoneID, zoneName);
                zones.Add(zoneID, zone);

                foreach (XmlNode bossNode in zoneNode.ChildNodes)
                {
                    int bossID = int.Parse(bossNode.Attributes["id"].Value);
                    string bossName = bossNode.Attributes["name"].Value;

                    Boss boss = new Boss(bossID, bossName);
                    zone.Bosses.Add(bossID, boss);

                    foreach (XmlNode itemNode in bossNode.ChildNodes)
                    {
                        Item item = new Item(int.Parse(itemNode.Attributes["id"].Value), itemNode.Attributes["name"].Value);
                        item.ZoneID = zoneID;
                        item.ZoneName = zoneName;
                        item.BossID = bossID;
                        item.BossName = bossName;

                        boss.Items.Add(item.ID, item);
                        zone.Items.Add(item.ID, item);
                    }
                }
            }

            return zones;
        }

        internal static Players LoadPlayers()
        {
            Players players = new Players();

            string xml_adress = "http://junemann.nl/null/getdata.php";
            XmlDocument doc = new XmlDocument();
            doc.Load(xml_adress);

            foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
            {
                players.AddPlayer(int.Parse(node.Attributes["id"].Value), node.Attributes["name"].Value, int.Parse(node.Attributes["pos"].Value));

                List<LootItem> items = players[players.ListOfPlayers.Count-1].Items;

                foreach (XmlNode itemNode in node.ChildNodes)
                {
                    bool isOffspec = (itemNode.Attributes["isoffspec"].Value == "1");

                    LootItem item = new LootItem(
                        int.Parse(itemNode.Attributes["id"].Value),
                        int.Parse(itemNode.Attributes["itemid"].Value),
                        DateTime.Parse(itemNode.Attributes["datetime"].Value),
                        isOffspec);

                    items.Add(item);                    
                }
            }
            return players;
        }

        /// <summary>
        /// Updates all players.
        /// </summary>
        /// <param name="players">The players which needs to be updated</param>
        internal static void UpdatePlayers(Players players)
        {
            foreach (Player player in players.ListOfPlayers)
            {
                UpdatePlayer(player);
            }
        }

        /// <summary>
        /// Update a specific player.
        /// </summary>
        /// <param name="player">The player to update</param>
        internal static void UpdatePlayer(Player player)
        {
            string xml_adress = "http://junemann.nl/null/saveplayer.php?id=" + player.ID + "&name=" + player.Name + "&pos=" + player.Position;
            XmlDocument doc = new XmlDocument();
            doc.Load(xml_adress);

            // There is always one playernode only, but we'll loop anyway.
            foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
            {
                player.ID = int.Parse(node.Attributes["id"].Value);
            }
        }

        /// <summary>
        /// Update lootitem.
        /// </summary>
        /// <param name="item">The lootitem to update.</param>
        /// <param name="playerID">The player who posseses the item</param>
        internal static void UpdateLootItem(LootItem item, int playerID)
        {
            int isOffspec = 0;
            if (item.IsOffspec) { isOffspec = 1; }

            string xml_adress = "http://junemann.nl/null/savelootitem.php?id=" + item.ID + "&datetime=" + item.DateTime + "&characterid=" + playerID
                + "&itemid=" + item.ItemID + "&isoffspec=" + isOffspec;
            XmlDocument doc = new XmlDocument();
            doc.Load(xml_adress);

            // There is always one playernode only, but we'll loop anyway.
            foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
            {
                item.ID = int.Parse(node.Attributes["id"].Value);
            }
        }

        /// <summary>
        /// Deletes a player.
        /// </summary>
        /// <param name="player">The player which needs to be deleted</param>
        internal static void DeletePlayer(Player player)
        {
            string xml_adress = "http://junemann.nl/null/deleteplayer.php?id=" + player.ID;
            XmlDocument doc = new XmlDocument();
            doc.Load(xml_adress);

            // If the xml fragments has items it's an error. Throw an exception.
            foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
            {
                throw new Exception("Character not deleted");
            }
        }


        /// <summary>
        /// Deletes a player.
        /// </summary>
        /// <param name="player">The player which needs to be deleted</param>
        internal static void DeleteLootItem(LootItem item)
        {
            string xml_adress = "http://junemann.nl/null/deletelootitem.php?id=" + item.ID;
            XmlDocument doc = new XmlDocument();
            doc.Load(xml_adress);

            // If the xml fragments has items it's an error. Throw an exception.
            foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
            {
                throw new Exception("Item not deleted");
            }
        }
    }
}
